import { 获取机构树 } from '../../../pages/index/technology/data/dict';
import { Cell, CellProps } from '../../doc/cell';
import { PopList } from '@knno/ui/popList';

const options = 获取机构树();

export function organ(cell: Cell, attrs: CellProps) {
	function openSelect() {
		PopList.show(
			options,
			{
				text: 'name',
				value: 'code',
				children: 'children',
				expand: 'expand',
			},
			{
				refer: cell,
				activeValue: cell.getValue()?.code,
				multiSelect: false,
				useSearch: true,
				resultFullPath: true,
				focus: true,
			}
		).onSelect((value, text) => {
			cell.setValue({ code: value, name: text });
			cell.text(text + '');
		});
	}
	cell.text(cell.getValue()?.name ?? '');
	cell.addClass('select');
	cell.on('click', () => {
		if (!attrs.readonly) {
			openSelect();
		}
	});
	cell.on('keydown', (evt) => {
		if (evt.key === ' ') {
			evt.preventDefault();
			evt.stopPropagation();
		}
	});
	cell.on('keyup', (evt) => {
		if (evt.key === ' ' || evt.key === 'Space' || evt.key === 'Enter') {
			evt.preventDefault();
			evt.stopPropagation();
			if (!attrs.readonly) {
				openSelect();
			}
		}
	});
}
